<?php
/**
 * a class used to generate fake data
 *
 * @author tom.wang<tom.wang.sz@qq.com>
 */
class DCenter {
	private static $cfg;
	private static $ddc;
	private static $dsc;
	private static $doc;
	private static $defineCfg;
	
	/**
	 * the entry to get data
	 *
	 * @author tom.wang<tom.wang.sz@qq.com>
	 * @param mixed $dataDefine
	 * @param array $cfg
	 *
	 * @return mixed
	 */
	public static function data($defineCfg = null, $cfg = array()) {
		self::setDefineCfg($defineCfg);
		self::setCfg($cfg);
		self::initial();
		return self::process();
	}
	
	/**
	 * set config
	 *
	 * @author tom.wang<tom.wang.sz@qq.com>
	 * @param array $cfg
	 *
	 * @return void
	 */
	public static function setCfg($cfg = array()) {
		self::$cfg = require FRAMEWORK_DCENTER_ROOT . 'config.php';
		foreach($cfg as $key => $val) {
			if(array_key_exists($key, self::$cfg)) self::$cfg[$key] = $val;
		}
	}
	
	public static function getCfg($item = null) {
		$ret = null;
		if(null === $item) $ret = self::$cfg;
		elseif(array_key_exists($item, self::$cfg)) $ret = self::$cfg[$item];
		
		return $ret;
	}
	
	private static function setDefineCfg($defineCfg) {
		self::$defineCfg = $defineCfg;
	}
	
	private static function initial() {
		$datasource = self::getCfg('datasource');
		$datadefine = self::getCfg('datadefine');
		$dataoutput = self::getCfg('dataoutput');
		
		if(null !== $datasource) self::$dsc = new DataSourceContext($datasource);
		if(null !== $datadefine) self::$ddc = new DataDefineContext($datadefine);
		if(null !== $dataoutput) self::$doc = new DataOutputContext($dataoutput);
	}
	
	private static function process() {
		# handle define
		$defineCfg = self::$ddc->parse(self::$defineCfg);
		
		# handle source
		$dataSource = self::$dsc->handle($defineCfg);
		
		# handle output
		$dataOutput = self::$doc->handle($dataSource);
		
		return $dataOutput;
	}
}
?>